<template>
  <div class="upbasicMsg-content" v-loading="loading">
    <el-row style="margin-top: 20px">
      <el-col :offset="0" :span="12">
        <aps-form
          :config="apsForm.formConfig"
          :model="apsForm.formData"
          :formRef="apsForm.formRef"
          :labelWidth="apsForm.labelWidth"
          ref="form"
        ></aps-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getByDeptId, getUserListByDept } from "@/api/departmental/index";
export default {
  props: {},
  components: {},
  data() {
    return {
      loading: true,
      parentId: null,
      parentName: "",
      deptPeopleArr: [],
      apsForm: {
        formRef: "form",
        formData: {},
        labelWidth: "160px",
        formConfig: {
          formAttr: {
            // inline: true,
            "label-position": "right",
            size: "small",
            bsColumn: {
              col: 2,
              middle: true,
            },
            rules: {},
          },
          properties: {
            companyName: {
              label: "单位名称:",
              typeName: "aps-des",
            },
            statisticsLeaderPostCode: {
              label: "邮政编码:",
              typeName: "aps-des",
            },
            companyAddressProvince: {
              bsColumn: {
                col: 1,
                middle: true,
              },
              label: "详细地址:",
              typeName: "aps-des",
            },

            statisticsLeaderId: {
              label: "统计负责人:",
              typeName: "aps-des",
            },
            statisticsLeaderMobilePhone: {
              label: "手机:",
              typeName: "aps-des",
            },
            statisticsLeaderFax: {
              label: "传真号码:",
              typeName: "aps-des",
            },
            statisticsLeaderEmail: {
              label: "电子邮箱:",
              typeName: "aps-des",
            },

            headOfUnitId: {
              label: "单位负责人:",
              typeName: "aps-des",
            },
            headOfUnitMobilePhone: {
              label: "手机:",
              typeName: "aps-des",
            },

            statisticsPersonId: {
              label: "统计员:",
              typeName: "aps-des",
            },
            statisticsPersonMobilePhone: {
              label: "手机:",
              typeName: "aps-des",
            },
          },
        },
      },
    };
  },
  created() {
    this.userDeptjudge();
  },
  mounted() {
    setTimeout(() => {
      this.getPeopleList();
    }, 200);
    setTimeout(() => {
      this.getDeptmsg();
    }, 400);
  },
  methods: {
    userDeptjudge() {
      let user = this.$store.state.user;
      this.parentId = user.deptparentId ?? 0;
      this.parentName = user.deptparentName ?? "暂无单位";
    },
    getDeptmsg() {
      this.loading = true;
      let params = {
        deptId: this.parentId,
      };
      getByDeptId(params)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let data = res?.data ?? {};
          if (Object.keys(data).length > 0) {
            this.$refs?.form?.setFieldValue(
              "companyName",
              data?.companyName ?? null
            );
            this.$refs?.form?.setFieldValue(
              "statisticsLeaderPostCode",
              data?.statisticsLeaderPostCode ?? null
            );
            this.$refs?.form?.setFieldValue(
              "companyAddressProvince",
              data?.companyAddressProvince ?? null
            );

            let statisticsLeaderId = this.findPeoplename(
              data?.statisticsLeaderId ?? null
            );
            this.$refs?.form?.setFieldValue(
              "statisticsLeaderId",
              statisticsLeaderId
            );
            this.$refs?.form?.setFieldValue(
              "statisticsLeaderMobilePhone",
              data?.statisticsLeaderMobilePhone ?? null
            );
            this.$refs?.form?.setFieldValue(
              "statisticsLeaderFax",
              data?.statisticsLeaderFax ?? null
            );
            this.$refs?.form?.setFieldValue(
              "statisticsLeaderEmail",
              data?.statisticsLeaderEmail ?? null
            );

            let headOfUnitId = this.findPeoplename(data?.headOfUnitId ?? null);
            this.$refs?.form?.setFieldValue("headOfUnitId", headOfUnitId);
            this.$refs?.form?.setFieldValue(
              "headOfUnitMobilePhone",
              data?.headOfUnitMobilePhone ?? null
            );

            let statisticsPersonId = this.findPeoplename(
              data?.statisticsPersonId ?? null
            );
            this.$refs?.form?.setFieldValue(
              "statisticsPersonId",
              statisticsPersonId
            );
            this.$refs?.form?.setFieldValue(
              "statisticsPersonMobilePhone",
              data?.statisticsPersonMobilePhone ?? null
            );
          } else {
            this.$refs?.form?.setFieldsValue({
              companyName: "暂无信息",
              statisticsLeaderPostCode: "暂无信息",
              companyAddressProvince: "暂无信息",
              statisticsLeaderId: "暂无信息",
              statisticsLeaderMobilePhone: "暂无信息",
              statisticsLeaderFax: "暂无信息",
              statisticsLeaderEmail: "暂无信息",
              headOfUnitId: "暂无信息",
              headOfUnitMobilePhone: "暂无信息",
              statisticsPersonId: "暂无信息",
              statisticsPersonMobilePhone: "暂无信息",
            });
          }
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!200370");
        });
    },
    getPeopleList() {
      this.deptPeopleArr = [];
      let params = {
        deptId: this.parentId,
      };
      getUserListByDept(params)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let obj = {
                label: item?.nickName ?? "未知用户昵称",
                value: item?.userId ?? 1,
              };
              this.deptPeopleArr.push(obj);
            });
          }
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!200380");
        });
    },
    findPeoplename(falge) {
      let result = "";
      let num = 0;
      if (falge != null) {
        if (this.deptPeopleArr.length > 0) {
          this.deptPeopleArr.map((item) => {
            if (item.value == falge) {
              result = item.label;
            } else {
              num++;
            }
          });
        } else {
          result = "未找到该人员";
        }
        if (num == this.deptPeopleArr.length) {
          result = "未找到该人员";
        }
      } else {
        result = "该人员不存在";
      }
      return result;
    },
  },
};
</script>

<style lang="scss" scoped>
.upbasicMsg-content {
  width: 100%;
}
</style>
